export default {
  formDataLoopAppen: (formdata, data) => {
    for (let i in data) {
      formdata.append(i, data[i])
    }
    return formdata
  },
  changeDateToString: (DateIn) => {
    // debugger
    var Year = 0;
    var Month = 0;
    var Day = 0;
    var CurrentDate = "";
    //初始化时间
    Year = DateIn.getFullYear()
    Month = DateIn.getMonth() + 1
    Day = DateIn.getDate()

    CurrentDate = Year + "-";
    if (Month >= 10) {
      CurrentDate = CurrentDate + Month + "-";
    } else {
      CurrentDate = CurrentDate + "0" + Month + "-";
    }
    if (Day >= 10) {
      CurrentDate = CurrentDate + Day;
    } else {
      CurrentDate = CurrentDate + "0" + Day;
    }

    return CurrentDate;
  },
  curentTime() {
    var now = new Date();
    var year = now.getFullYear(); //年
    var month = now.getMonth() + 1; //月
    var day = now.getDate(); //日
    var hh = now.getHours(); //时
    var mm = now.getMinutes(); //分
    var clock = year + "年";

    if (month < 10)
      clock += "0";

    clock += month + "月";

    if (day < 10)
      clock += "0";

    clock += day + "日";

    if (hh < 10)
      clock += "0";

    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm;
    return (clock);
  },
  translate: (str, type = "engToChina") => {
    let factorDiction = {
      zinc: '总锌',
      nickel: '总镍',
      chromium: '总铬',
      nitrogen: '总氮',
      phosphorus: '总磷',
      cuprum: '总铜',
      prussiate: '总氰化物',
      ammoniaNitrogen: '氨氮',
      pH: 'pH',
      ph: 'pH',
      // COD: 'CODcr',
      COD: 'CODcr',
      nickelFlag: '总镍监测设备',
      chromiumFlag: '总铬监测设备',
      nitrogenFlag: '总氮监测设备',
      phosphorusFlag: '总磷监测设备',
      cuprumFlag: '总铜监测设备',
      prussiateFlag: '总氰化物监测设备',
      ammoniaNitrogenFlag: '氨氮监测设备',
      zincFlag: '总锌监测设备',
      pHFlag: 'pH监测设备',
      week: '周',
      year: '年',
      month: '月',

      codAlert: 'COD设备告警',
      zincAlert: '总锌设备告警',
      nickelAlert: '总镍设备告警',
      chromiumAlert: '总铬设备告警',
      nitrogenAlert: '总氮设备告警',
      phosphorusAlert: '总磷设备告警',
      cuprumAlert: '总铜设备告警',
      prussiateAlert: '总氰化物设备告警',
      ammoniaNitrogenAlert: '氨氮设备告警',
      pHAlert: 'pH设备告警',
      systemAlarm: "系统设备告警",
      missingFactor: "缺失因子",
      reason: "离线详情"
    }
    if (type === "engToChina") {
      return factorDiction[str]
    }
    if (type === "chinaToEng") {
      for (var i in factorDiction) {
        if (factorDiction[i] === str) {
          return i
        }
      }
    }
  },
  deepClone: (target) => {
    // return JSON.parse(JSON.stringify(obj))
    let copyed_objs = []; //此数组解决了循环引用和相同引用的问题，它存放已经递归到的目标对象 
    function _deepCopy(target) {
      if ((typeof target !== 'object') || !target) {
        return target;
      }
      for (let i = 0; i < copyed_objs.length; i++) {
        if (copyed_objs[i].target === target) {
          return copyed_objs[i].copyTarget;
        }
      }
      let obj = {};
      if (Array.isArray(target)) {
        obj = []; //处理target是数组的情况 
      }
      copyed_objs.push({
        target: target,
        copyTarget: obj
      })
      Object.keys(target).forEach(key => {
        if (obj[key]) {
          return;
        }
        obj[key] = _deepCopy(target[key]);
      });
      return obj;
    }
    return _deepCopy(target);
  },
  /**
   * @param {Function} func
   * @param {number} wait
   * @param {boolean} immediate
   * @return {*}
   */
  debounce: (func, wait, immediate) => {
    let timeout, args, context, timestamp, result

    const later = function () {
      // 据上一次触发时间间隔
      const last = +new Date() - timestamp

      // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
      if (last < wait && last > 0) {
        timeout = setTimeout(later, wait - last)
      } else {
        timeout = null
        // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
        if (!immediate) {
          result = func.apply(context, args)
          if (!timeout) context = args = null
        }
      }
    }

    return function (...args) {
      context = this
      timestamp = +new Date()
      const callNow = immediate && !timeout
      // 如果延时不存在，重新设定延时
      if (!timeout) timeout = setTimeout(later, wait)
      if (callNow) {
        result = func.apply(context, args)
        context = args = null
      }

      return result
    }
  },
  /**
   * @param {Number} value 超标值
   * @param {String} standard 标准值
   * @return {Number} 超标倍数
   */
  multiples: (value, standard) => {
    if (typeof standard === "object") {
      if (value > standard.max) {
        return ((value - standard.max) / standard.max).toFixed(2)
      } else if (value < standard.min) {
        return ((standard.min - value) / standard.min).toFixed(2)
      }
    } else {
      return ((value / standard) - 1).toFixed(2)
    }
  },
  /* 时间格式处理 */
}
